package com.hp.service;

import com.google.common.util.concurrent.AtomicDouble;
import com.hp.dao.HouseMapper;
import com.hp.dao.SellMapper;
import com.hp.pojo.Concern;
import com.hp.pojo.House;
import com.hp.pojo.Renting;
import com.hp.pojo.Sell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SellService {
    @Autowired private SellMapper sellMapper;
    @Autowired private HouseMapper houseMapper;
    @Autowired private ConcernService concernService;
    @Autowired private RentingService rentingService;

    public Map<String ,Object> getAllSells(){
        HashMap<String,Object> allSells = new HashMap<>();
        allSells.put("allSells",sellMapper.getAllSells());
        return allSells;
    }

    public Map<String ,Object> getSellsAll(){
        HashMap<String,Object> allSells = new HashMap<>();
        allSells.put("allSells",sellMapper.getSellsAll());
        return allSells;
    }

    public Map<String, String> changeApply(int sellId) {
        //修改某卖房记录的申请状态
        Sell tempSell = sellMapper.getSellById(sellId);

        if(tempSell.getSellApply() == 1){
            tempSell.setSellApply(0);
        }
        else if(tempSell.getSellApply() == 0){
            tempSell.setSellApply(1);
        }
        if(tempSell.getSellState() == 1){
            tempSell.setSellState(0);
        }
        else if(tempSell.getSellState() == 0){
            tempSell.setSellState(1);
        }

        sellMapper.updateSellById(tempSell);
        HashMap<String, String> res = new HashMap<>();
        res.put("message","修改成功");
        return res;
    }

    public Map<String, String> addSell(Sell sell) {
        HashMap<String, String> res = new HashMap<>();
        try {
            sellMapper.addSell(sell);
            res.put("message","添加成功");
            return res;
        }
        catch (Exception e){
            res.put("message","添加失败");
            return res;
        }
    }

    public Map<String, Object> getAllSellsByHouseId(int houseId) {
        List<Sell> allSells = sellMapper.getAllSells();
        List<Sell> res = new ArrayList<>();
        for(Sell temp:allSells){
            if(temp.getHouseId() == houseId){
                res.add(temp);
            }
        }

        HashMap<String,Object> allSellsByHouseId = new HashMap<>();
        allSellsByHouseId.put("allSells",res);
        return allSellsByHouseId;
    }

    public Map<String, Object> getAllSell_houses() {
        List<Sell> allSell = null;
        allSell = sellMapper.getAllSells();

        List<House> allSell_house = new ArrayList<>();
        for(Sell temp:allSell){
            allSell_house.add(
                    houseMapper.getHouseById(temp.getHouseId())
            );
        }

        HashMap<String,Object> res = new HashMap<>();
        res.put("allSell_house",allSell_house);
        return res;
    }

    public Map<String, Object> getRecommandHouse_sell(int userId) {
        /*
        获取所有再买的sells
        获取所有对应的房子
        获取用户关注的信息
        通过房子信息计算P_C、D_C、F_C
        计算目前所有房子的P D F
        排序
         */
        List<House> allSell_house = (List<House>) getAllSell_houses().get("allSell_house");
        List<Concern> allCon = (List<Concern>) concernService.getALlConcern(userId).get("oneconcern");

//        获取所有用户关注的房子
        List<House> all_con_house = new ArrayList<>();
        for(Concern con: allCon){
            House temp = houseMapper.getHouseById(con.getHouseId());
            all_con_house.add(temp);
        }

//      计算PC,DC,FC
        double PC = 0,FC_NUM_WO = 0,FC_NUM_KE = 0 ,FC_NUM_CEN = 0;

        for(House temp:all_con_house){
            List<Sell> sellTemp = (List<Sell>) getAllSellsByHouseId(temp.getHouseId()).get("allSells");
            PC += sellTemp.get(0).getSellPrice();
            FC_NUM_WO += temp.getHouseBedroomnum();
            FC_NUM_KE += temp.getHouseLivingnum();
            FC_NUM_CEN += temp.getHouseFloor();
        }
        PC = PC/all_con_house.size();
        FC_NUM_WO = FC_NUM_WO/all_con_house.size();
        FC_NUM_KE = FC_NUM_KE/all_con_house.size();
        FC_NUM_CEN = FC_NUM_CEN/all_con_house.size();
        String DC = getDC(all_con_house);

        AtomicDouble PC_ = new AtomicDouble(PC);
        AtomicDouble FC_NUM_WO_ = new AtomicDouble(FC_NUM_WO);
        AtomicDouble FC_NUM_KE_ = new AtomicDouble(FC_NUM_KE);
        AtomicDouble FC_NUM_CEN_ = new AtomicDouble(FC_NUM_CEN);
        System.out.println("hh:"+allSell_house);
//        AtomicDouble DC_ = new Atomic(DC);
//        allSell_house.sort((x1,x2) -> (getScore(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC)
//                - getScore(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC)));
        allSell_house.sort((x1,x2) -> {
            return (getScore(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC)
                    - getScore(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC));
        });

        //==================================================================================

        List<Renting> allRenting = (List<Renting>) rentingService.getAllRentings().get("allRentings");

        List<House> allRenting_house = new ArrayList<>();
        for (Renting r:allRenting){
            allRenting_house.add(houseMapper.getHouseById(r.getHouseId()));
        }
        allRenting_house.sort((x1,x2) -> (getScore2(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC)
                - getScore2(x1,PC_,FC_NUM_WO_,FC_NUM_KE_,FC_NUM_CEN_,DC)));

        HashMap<String,Object> res = new HashMap<>();
        res.put("sells",allRenting_house);
        res.put("rentings",allSell_house);
        return res;

    }

    private int getScore(House x, AtomicDouble pc_, AtomicDouble fc_num_wo_, AtomicDouble fc_num_ke_, AtomicDouble fc_num_cen_, String dc) {
        System.out.println(getAllSellsByHouseId(x.getHouseId()));
        List<Sell> sellTemp = (List<Sell>) getAllSellsByHouseId(x.getHouseId()).get("allSells");
        double PC = sellTemp.get(0).getSellPrice();
        double FC_NUM_WO = x.getHouseBedroomnum();
        double FC_NUM_KE = x.getHouseLivingnum();
        double FC_NUM_CEN = x.getHouseFloor();
        double DC;
        if(x.getHouseRegion().equals(dc)){
            DC = 1;
        }else{
            DC=0;
        }
        int W[] = {10,5,5,5,10};
        return (int) (W[0]*PC+W[1]*FC_NUM_WO+W[2]*FC_NUM_KE+W[3]*FC_NUM_CEN+DC*W[4]);
    }

    private int getScore2(House x, AtomicDouble pc_, AtomicDouble fc_num_wo_, AtomicDouble fc_num_ke_, AtomicDouble fc_num_cen_, String dc) {
        List<Renting> sellTemp = (List<Renting>) rentingService.getAllRentingsByHouseId(x.getHouseId()).get("allRentings");
        double PC = sellTemp.get(0).getRentingPrice();
        double FC_NUM_WO = x.getHouseBedroomnum();
        double FC_NUM_KE = x.getHouseLivingnum();
        double FC_NUM_CEN = x.getHouseFloor();
        double DC;
        if(x.getHouseRegion().equals(dc)){
            DC = 1;
        }else{
            DC=0;
        }
        int W[] = {10,5,5,5,10};
        return (int) (W[0]*PC+W[1]*FC_NUM_WO+W[2]*FC_NUM_KE+W[3]*FC_NUM_CEN+DC*W[4]);
    }


    private String getDC(List<House> all_con_house) {
        /*
        找到所有房子中最频繁所在的区
         */
        HashMap<String,Integer> temp = new HashMap<>();
        List<String> all_reg = new ArrayList<>();
        for(House house:all_con_house){
            String reg = house.getHouseRegion();
            all_reg.add(reg);
            if(temp.containsKey(reg)){
                temp.put(reg,temp.get(reg) + 1);
            }
            else{
                temp.put(reg,0);
            }
        }
        String KEY = null;
        int VAL = -1;
        for(String reg:all_reg){
            if(temp.get(reg) > -1){
                KEY = reg;
                VAL = temp.get(reg);
            }
        }
        return KEY;
    }

    public Map<String, Object> getAgentSell() {
        HashMap<String,Object> map=new HashMap<>();
        map.put("agentSells",sellMapper.getAgentSell());
        map.put("noagentSells",sellMapper.getNoAgentSell());
        return map;
    }
}
